package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.vo.LeadVo;
import com.opgea.crm.domain.entities.Lead;
import com.opgea.crm.domain.entities.Leadphones;
import com.opgea.crm.domain.modal.QualifierModel;
import com.opgea.crm.repository.LeadRepo;

@Service
public class LeadServiceImpl implements LeadService{

	@Autowired
	private LeadRepo leadRepo;
	
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void createLead(Lead lead) {
		lead.setLeadStatus(ApplicationConstant.LeadStatusQualifier.Lead);
    	lead.setActiveStatus(ApplicationConstant.ActiveQualifier.Active);
		leadRepo.create(lead);
	}
	
	@Transactional
	public void update(Lead lead) {
		leadRepo.update(lead);
	}	

	public void removeLead(long id) {
		// TODO Auto-generated method stub
	}

	public Lead findLead(long id) {
		return leadRepo.find(id);
	}

	public List<QualifierModel> getLeadSearchCriteriaTypes(){
		List<QualifierModel> leadSearchTypes = new ArrayList<QualifierModel>();
		for(ApplicationConstant.LeadSearchCriteriaQualifier qualifier : ApplicationConstant.LeadSearchCriteriaQualifier.values()){
			leadSearchTypes.add(new QualifierModel(Long.valueOf(qualifier.ordinal()), qualifier.name()));
			System.out.println("Lead Search Type : "+qualifier);
		}
		return leadSearchTypes;
	}	
	
	public List<LeadVo> findAll() {
		
		List<LeadVo> list=new ArrayList<LeadVo>();
		List<Lead> leadList = leadRepo.findAll();
    	for(Lead data: leadList)
    	{
    		LeadVo leadVo=new LeadVo();
    		leadVo.setId(data.getId());
    		if(data.getFirstName() != null){
    			leadVo.setFirstName(data.getFirstName());
    		}
    		if(data.getMiddleInitial() != null){
    			leadVo.setMiddleInitial(data.getMiddleInitial());
    		}
    		if(data.getLastName() != null){
    			leadVo.setLastName(data.getLastName());
    		}
    		if(data.getTitle() != null){
    			leadVo.setTitle(data.getTitle());
    		}
    		if(data.getLeademail() !=null){
     		   	leadVo.setEmail(data.getLeademail().getAddress());
     		}
     		if(data.getCompany() != null){
     			leadVo.setCompanyName(data.getCompany().getName());
     		}
     		
     		List<Leadphones> phones = new ArrayList<Leadphones>(data.getLeadphonesCollection());
     		if(phones.size() > 0){
     			leadVo.setPhone1(phones.get(0).getPhoneNo());
     		}
     		if(phones.size() > 1){
     			leadVo.setPhone2(phones.get(1).getPhoneNo());
     		}
    		list.add(leadVo);
    		//System.out.println("Lead "+list.indexOf(leadVo)+": "+leadVo);
    	}
    	return list;
	}
	
	public List<LeadVo> findDynamically(String criteria, String value) {
		List<LeadVo> list=new ArrayList<LeadVo>();
		List<Lead> leadList = leadRepo.findDynamically(criteria, value, ApplicationConstant.LeadStatusQualifier.Lead);
		
    	for(Lead data: leadList)
    	{
    		LeadVo leadVo=new LeadVo();
    		leadVo.setId(data.getId());
    		if(data.getFirstName() != null){
    			leadVo.setFirstName(data.getFirstName());
    		}
    		if(data.getMiddleInitial() != null){
    			leadVo.setMiddleInitial(data.getMiddleInitial());
    		}
    		if(data.getLastName() != null){
    			leadVo.setLastName(data.getLastName());
    		}
    		if(data.getTitle() != null){
    			leadVo.setTitle(data.getTitle());
    		}
    		if(data.getLeadprofile()!= null){
    			leadVo.setGrade(data.getLeadprofile().getGrade().getFieldValue());
    		}
    		if(data.getLeadprofile() != null){
    			leadVo.setStage(data.getLeadprofile().getStage().getFieldValue());
    	    }
    		if(data.getLeadprofile() != null){
    			leadVo.setAssignedTo(data.getLeadprofile().getEmployee().getFirstName());
    		}
    		if(data.getLeademail() !=null){
    			leadVo.setEmail(data.getLeademail().getAddress());
    		}
    		if(data.getCompany() != null){
    			leadVo.setCompanyName(data.getCompany().getName());
    		}
    		
    		List<Leadphones> phones = new ArrayList<Leadphones>(data.getLeadphonesCollection());
    		if(phones.size() > 0){
    		    leadVo.setPhone1(phones.get(0).getPhoneNo());
    		}
    		if(phones.size() > 1){
    		    leadVo.setPhone2(phones.get(1).getPhoneNo());
    		}
    		
    		//System.out.println(phones.get(0).getPhoneNo());
    		list.add(leadVo);
    	}
    	return list;
	}
	
	public List<LeadVo> findAllLeadCustomerDynamically(String criteria, String value) {
		List<LeadVo> list=new ArrayList<LeadVo>();
		List<Lead> leadList = leadRepo.findDynamically(criteria, value);
		
    	for(Lead data: leadList)
    	{
    		LeadVo leadVo=new LeadVo();
    		leadVo.setId(data.getId());
    		if(data.getFirstName() != null){
    			leadVo.setFirstName(data.getFirstName());
    		}
    		if(data.getMiddleInitial() != null){
    			leadVo.setMiddleInitial(data.getMiddleInitial());
    		}
    		if(data.getLastName() != null){
    			leadVo.setLastName(data.getLastName());
    		}
    		if(data.getTitle() != null){
    			leadVo.setTitle(data.getTitle());
    		}
    		if(data.getLeadprofile()!= null){
    			leadVo.setGrade(data.getLeadprofile().getGrade().getFieldValue());
    		}
    		if(data.getLeadprofile() != null){
    			leadVo.setStage(data.getLeadprofile().getStage().getFieldValue());
    	    }
    		if(data.getLeadprofile() != null){
    			leadVo.setAssignedTo(data.getLeadprofile().getEmployee().getFirstName());
    		}
    		if(data.getLeademail() !=null){
    			leadVo.setEmail(data.getLeademail().getAddress());
    		}
    		if(data.getCompany() != null){
    			leadVo.setCompanyName(data.getCompany().getName());
    		}
    		List<Leadphones> phones = new ArrayList<Leadphones>(data.getLeadphonesCollection());
    		if(phones.size() > 0){
    		    leadVo.setPhone1(phones.get(0).getPhoneNo());
    		}
    		if(phones.size() > 1){
    		    leadVo.setPhone2(phones.get(1).getPhoneNo());
    		}
    		
    		//System.out.println(phones.get(0).getPhoneNo());
    		list.add(leadVo);
    	}
    	return list;
	}
	
	public List<QualifierModel> findAllLeadName(){
		
		List<Lead> leads = leadRepo.findAll();
		List<QualifierModel> leadNameList = new ArrayList<QualifierModel>();
		for(Lead lead : leads){
			leadNameList.add(new QualifierModel(lead.getId(), lead.getFirstName()));
			System.out.println("Lead Name : "+ lead.getFirstName());
		}
		return leadNameList;
	}

	public List<Lead> findAllByCompanyId(long companyId) {
		return leadRepo.findAllByCompanyId(companyId);
	}

	public List<QualifierModel> findAllLeadNameByCompanyId(long companyId) {
		List<Lead> leads = leadRepo.findAllByCompanyId(companyId);
		List<QualifierModel> leadNameList = new ArrayList<QualifierModel>();
		for(Lead lead : leads){
			leadNameList.add(new QualifierModel(lead.getId(), lead.getFirstName()));
		}
		return leadNameList;
	}
	
	public List<QualifierModel> getDummyCountries(){
		List<QualifierModel> countries = new ArrayList<QualifierModel>();
		countries.add(new QualifierModel(Long.valueOf(1), "India"));
		countries.add(new QualifierModel(Long.valueOf(2), "Japan"));
		countries.add(new QualifierModel(Long.valueOf(3), "South Africa"));
		countries.add(new QualifierModel(Long.valueOf(4), "USA"));
		return countries;
	}
	
	public List<QualifierModel> getDummyProvinces(){
		List<QualifierModel> cities = new ArrayList<QualifierModel>();
		cities.add(new QualifierModel(Long.valueOf(1), "Noida"));
		cities.add(new QualifierModel(Long.valueOf(2), "Durban"));
		cities.add(new QualifierModel(Long.valueOf(3), "California"));
		cities.add(new QualifierModel(Long.valueOf(4), "Tokyo"));
		return cities;
	}	
	
	public List<QualifierModel> getIndustryTypes(){
		List<QualifierModel> industries = new ArrayList<QualifierModel>();
		for(ApplicationConstant.IndustryQualifier qualifier : ApplicationConstant.IndustryQualifier.values()){
			industries.add(new QualifierModel(Long.valueOf(qualifier.ordinal()), qualifier.name()));
		}
		return industries;
	}

	@Override
	public List<LeadVo> findLeadList(Long assignedToId,Long gradeId,Long stageId) {
		List<Lead> leads = leadRepo.findLeadList(assignedToId,gradeId,stageId);
		List<LeadVo> leadVoList = new ArrayList<LeadVo>();
		for(Lead lead : leads){
			LeadVo leadVo=new LeadVo();
    		leadVo.setId(lead.getId());
    		if(lead.getFirstName() != null){
    			leadVo.setFirstName(lead.getFirstName());
    		}
    		if(lead.getMiddleInitial() != null){
    			leadVo.setMiddleInitial(lead.getMiddleInitial());
    		}
    		if(lead.getLastName() != null){
    			leadVo.setLastName(lead.getLastName());
    		}
    		if(lead.getTitle() != null){
    			leadVo.setTitle(lead.getTitle());
    		}
    		if(lead.getLeadprofile().getGrade()!= null){
    			leadVo.setGrade(lead.getLeadprofile().getGrade().getFieldValue());
    		}
    		if(lead.getLeadprofile().getStage() != null){
    			leadVo.setStage(lead.getLeadprofile().getStage().getFieldValue());
    	    }
    		if(lead.getLeadprofile().getEmployee() != null){
    			leadVo.setAssignedTo(lead.getLeadprofile().getEmployee().getFirstName());
    		}
    		if(lead.getLeademail() !=null){
    		   leadVo.setEmail(lead.getLeademail().getAddress());
    		}
    		if(lead.getCompany() != null){
    		   leadVo.setCompanyName(lead.getCompany().getName());
    		}
    		
    		List<Leadphones> phones = new ArrayList<Leadphones>(lead.getLeadphonesCollection());
    		if(phones.size() > 0){
    		    leadVo.setPhone1(phones.get(0).getPhoneNo());
    		}
    		if(phones.size() > 1){
    		    leadVo.setPhone2(phones.get(1).getPhoneNo());
    		}
    		
    		leadVoList.add(leadVo);
		}
		System.out.println("lead size : "+ leads.size());
		return leadVoList; 
	}
	
	
}
